package com.war.controller.warehousing;

import com.war.pojo.warehousing.PurchaseInfo;
import com.war.pojo.warehousing.ReturnInfo;
import com.war.service.safeManage.UserService;
import com.war.service.warehousing.PurchaseService;
import com.war.service.warehousing.ReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class ReturnController {

    @Autowired(required = false)
    ReturnService returnService;

    //访问退货首页
    @RequestMapping("/return")
    public String returnPage(ReturnInfo returnInfo,ModelMap m, HttpServletRequest request){
        HashMap<String,Object> map = returnService.selectReturnAll(returnInfo);
        //把数据传到前端
        m.put("info",map);
        //把用户的查询条件传回到前端
        m.put("preCondition",returnInfo.getCondition());
        m.put("start",returnInfo.getStartDate());
        m.put("end",returnInfo.getEndDate());
        return "warehousing/return";
    }

    //访问添加退货记录的方法
    @RequestMapping("/insertReturn")
    public String insertReturn( HttpServletRequest request){
        return "warehousing/return-add";
    }

    // 处理添加的ajax请求
    @RequestMapping("/insertReturnAjax")
    @ResponseBody
    public HashMap<String,Object> insertReturnAjax(ReturnInfo returnInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = returnService.insertReturn(returnInfo);
        map.put("info",result);
        return map;
    }

    //退货记录查看详情
    @RequestMapping("/returnDetail")
    public String returnDetail(ReturnInfo returnInfo,ModelMap m){
        //获得对应的退货记录
        PurchaseInfo reInfo = returnService.selectById(returnInfo);
        //把数据传到前端
        m.addAttribute("reInfo",reInfo);
        return "warehousing/return-detail";
    }

    //修改退货记录
    @RequestMapping("/returnEdit")
    public String returnEdit(ReturnInfo returnInfo,ModelMap m, HttpServletRequest request){
        //获得对应的采购记录
        ReturnInfo reInfo = returnService.selectById(returnInfo);
        //把数据传到前端
        m.addAttribute("reInfo",reInfo);
        return "warehousing/return-edit";
    }


    // 处理修改的ajax请求
    @RequestMapping("/editAjax")
    @ResponseBody
    public HashMap<String,Object> editAjax(ReturnInfo returnInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = returnService.editReturn(returnInfo);
        map.put("info",result);
        return map;
    }

    // 处理删除的ajax请求
    @RequestMapping("/delReturn")
    @ResponseBody
    public HashMap<String,Object> delReturn(ReturnInfo returnInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = returnService.delReturn(returnInfo);
        map.put("info",result);
        return map;
    }

    // 添加退货记录时的入库编号是否存在
    @RequestMapping("/existWarehousingIdAjax")
    @ResponseBody
    public HashMap<String,Object> existWarehousingIdAjax(PurchaseInfo purchaseInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = returnService.ifExistWarehousingId(purchaseInfo);
        map.put("info",result);
        return map;
    }

    //处理比较退货数量与入库数量的ajax请求
    @RequestMapping("/storageNumOver")
    @ResponseBody
    public HashMap<String,Object> storageNumOver(ReturnInfo returnInfo){
        HashMap<String,Object> map = new HashMap<>();
        int result = returnService.storageNumOver(returnInfo);
        map.put("info",result);
        return map;
    }

    //批量删除
    @RequestMapping(value="delAll",method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> delAll(@RequestBody List<ReturnInfo> returnInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = returnService.delAll(returnInfo);
        map.put("info",result);
        return map;
    }
}
